//==============================================================================
//
// Title:       PsuContorl.c
// Purpose:     A short description of the implementation.
//
// Created on:  2012-6-8 at 16:22:55 by luh.
// Copyright:   dafda. All Rights Reserved.
//
//==============================================================================

//==============================================================================
// Include files



//#include "PSU.h"
#include "RFBB.h"
#include "cvidef.h"
#include "N5700.h"

#include <cvintwrk.h>
#include "stdtst.h"
#include "tsutil.h"
#include <formatio.h>
#include <utility.h>
#include "PsuContorl.h"


static ErrMsg errMsg;
static ERRORINFO errorInfo;
static int error;
ViSession   g_vi_sInitHandleN5700_1;
int vi_sInit = 0;
ViRsrc virGPIBN = 0;
//char name[255] = {'\0'}; 
unsigned int uiSize = 0;


int InitPSU(void)
{
   int error = 0;          // required for tsErrChk
   char* pszTempBuff = 0;
   unsigned char szDeviceAddress[255] = {'\0'};
   unsigned char szDeviceAddressFmt[] = {"GPIB0::%d::%d"};

   int i = 0;
   // PSU0 (Agilent 5767A) initial******************************
      
  memset( szDeviceAddress,'\0', uiSize);       
   
  error = RegReadString (REGKEY_HKLM, "SOFTWARE\\NSN\\SWIFT\\Instruments\\PSU0", 
        "DeviceAddress", szDeviceAddress, sizeof(szDeviceAddress), &uiSize);
  if( error ) {
     pszTempBuff = GetGeneralErrorString(error);
     sprintf(errMsg, "PSU0\\DeviceAddress - %s", pszTempBuff);
     goto Error;
  }
  
  virGPIBN = ( ViRsrc) szDeviceAddress; 
  vi_sInit = N5700_Init(virGPIBN, VI_TRUE, VI_TRUE, &g_vi_sInitHandleN5700_1);
  if ( vi_sInit != 0){
      EELog_INEventLogLogEvent (g_hEventLog, NULL, " PSU0 Initialized failed.");
      error =-1;
      goto Error;
  }
  else  EELog_INEventLogLogEvent (g_hEventLog, NULL, " PSU0 Initialized passed."); 
  vi_sInit = N5700_Reset(g_vi_sInitHandleN5700_1);
  if ( vi_sInit != 0){
      EELog_INEventLogLogEvent (g_hEventLog, NULL, " PSU0 Rest failed.");
      error =-8;
      goto Error;
  }
  else  EELog_INEventLogLogEvent (g_hEventLog, NULL, " PSU0 Reset passed."); 
      

Error:
       if( error < 0 ) {
          vi_sInit = 1;
          MessagePopup("Power supply init", "Failed");          
        }
       return 0; 
}

int PSU_SetVoltage (ViSession   g_vi_sInitHandle, double dVoltage)
{
    int iReturn = 0;
    iReturn = N5700_SetVoltage(g_vi_sInitHandle, dVoltage);
    return iReturn;
}
int PSU_SetCurrent (ViSession   g_vi_sInitHandle, double dCurrentLimit)
{
    int iReturn = 0;
    iReturn = N5700_SetCurrent(g_vi_sInitHandle,dCurrentLimit);
    iReturn = N5700_SetCurrentProtectionState(g_vi_sInitHandle,VI_TRUE);
    return iReturn;
}    
int PSU_SetOutputState(ViSession   g_vi_sInitHandle, int iState)
{
    int iReturn = 0;
    ViBoolean vbUnregulate = 0;
    iReturn = N5700_SetOutputState(g_vi_sInitHandle, iState);

    return iReturn;
}    
int PSU_MeasureCurrent(ViSession   g_vi_sInitHandle, double* dCurrent)
{
    int iReturn = 0;
    iReturn = N5700_MeasureCurrent(g_vi_sInitHandle,dCurrent);
    return iReturn;
}

int PSU_GetCurrentProtectionStatus( ViSession   g_vi_sInitHandle, int *piOverCurrent)
{
    int iReturn = 0;    
    iReturn = N5700_GetCurrentProtectionStatus(g_vi_sInitHandle,piOverCurrent);
    return iReturn;

}
int PSU_ClearOutputProtection( ViSession   g_vi_sInitHandle)
{
    int iReturn = 0;
    iReturn = N5700_ClearOutputProtection(g_vi_sInitHandle);
    return iReturn;
}
int PSU_Close(ViSession   g_vi_sInitHandle)
{
    int iReturn = 0;
    iReturn = N5700_Close(g_vi_sInitHandle);
    return iReturn;

}

